<template>
	<div :key="uploadKey">
		<h3>用人单位信用承诺书</h3>
		<div style="margin: 20px 0">
			<el-button  text type="primary" v-if="!isDetail" @click="toDownload">下载模板</el-button>
		</div>
		<div  v-if="!isDetail">
			<AplidUpload
				v-model="modelData.imageList"
				:fileType= "uploadType.fileMetaType"
				listType="picture"
				:showFileList="true"
				name="用人单位信用承诺书"
				:limit="1"
				:multiple="false"
				:disabled="isDetail"
				@change="uploadChange"
			></AplidUpload>
		</div>
		<div style="width: 150px;" v-else>
			<FileList 
			:src="modelData.imageList"
			:only-ready="true"
			:showFileList="true"
			>
			</FileList>
		</div>
	</div>
</template>

<script setup lang="ts">

import { ref } from 'vue'
import AplidUpload from '@/components/AplidCrud/src/AplidUpload/index.vue'
import FileSaver from 'file-saver'
import apiMod from '@/apiMod/index'
import FileList from '@/components/AplidCrud/src/AplidUpload/PreviewImg/index.vue'
import { uploadType } from '@/utils/util'

const props = defineProps<{
	params?: any
	creditCode?: string | number
	editData?: any
	isDetail?: boolean
	caseId?: string | number
	custome?: boolean
	approvalStatus?: string | number
}>()
let modelData = reactive({imageList:''})

// 去下载模板
const toDownload = () => {
	FileSaver.saveAs('https://aplid-51.oss-cn-hangzhou.aliyuncs.com/1707027247636_7ca9c973.docx', '用人单位信用承诺书.docx')
}
let newCaseId = ref('')
const addSave = async() =>{
	if(props.params&&props.params.belongDate){
		let date = props.params.belongDate.split('-')
		let params0 = {
		applyYear:date[0],
		applyMonth:date[1],
		dataContent: {imageList:modelData.imageList},
		dataResults: {imageList:modelData.imageList},
		fromId:props.editData.formId,
		fromType: 'entry_org_promise',
		tableTemplateId: 'entry_org_promise',
		projectId: props.params.subsidyProjectId,
	}
	let res = await apiMod.view_formResults_add(params0)
	newCaseId.value = res.data
	}
}

const eidtSave =() => {
	let query = {
		id:props.editData.result_case_id || newCaseId.value,
		dataContent: {imageList:modelData.imageList},
		dataResults:  {imageList:modelData.imageList},
		fromId: props.editData.formId,
		fromType: 'entry_org_promise',
		tableTemplateId: 'entry_org_promise',
		projectId:props.params.subsidyProjectId,
	}
	apiMod.view_formResults_edit(query).then(res=>{
		ElMessage.success('修改成功')
	})
}
const uploadChange = (data) =>{
	eidtSave()
}
let uploadKey = ref(0)
onMounted(()=>{
	// 没有id就进行第一次保存
	if(!props.editData.result_case_id){
		addSave()
	}
	modelData = props.editData
	uploadKey.value = Math.random()
})

</script>
<style scoped lang="scss"></style>
